<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Setting extends CI_Controller {
// 
//  login.php
//  phpweb
//  
//  Created by WillLee on 2011-06-28.
//  Copyright 2011 lishuzu. All rights reserved.
// 


	function __construct()
	{
		parent::__construct();
		$this->load->model('user_model');
 		$this->load->model('common/security_model');
		if($this->security_model->isLogin() === FALSE){
			showmessage('请登录', 'login', 2);
		}
		
		$this->user = $this->session->userdata('user') ? $this->session->userdata('user') : '';
	}
	
	public function index()
	{	
		$data = array();
		if (submitcheck('submitsetting')) {
			$name= $this->input->post('name') ? $this->input->post('name', true) : '';
			$intro= $this->input->post('intro') ? $this->input->post('intro', true) : '';
			$myblog = $this->input->post('myblog') ? $this->input->post('myblog', true) : '';
			$mailnotice = $this->input->post('mailnotice') ? $this->input->post('mailnotice', true) : '0';
			if (!$this->user['namestatus']) {
				if (empty($name) || empty($intro)) {
					showmessage('姓名或描述未填写', 'setting/index', 2);
				}
				$updatedata = array(
					'name' => trim($name),
					'intro' => $intro,
					'myblog' => prep_url($myblog),
					'mailnotice' => intval($mailnotice),
					
				);
			}else{
				if (empty($intro)) {
					showmessage('姓名或描述未填写', 'setting/index', 2);
				}
				$updatedata = array(
					'intro' => $intro,
					'myblog' => prep_url($myblog),
					'mailnotice' => intval($mailnotice),
				);
			}	

				
			//取消html
			$updatedata = shtmlspecialchars($updatedata);

			$this->user_model->update($this->user['uid'], $updatedata);
			$this->security_model->update_user_session();
			showmessage('更新成功', 'setting/index', 1);
		}
		$data['userinfo'] = $this->user;
		$data['userinfo']['intro'] = str_replace("<br/>", "\r\n",  $data['userinfo']['intro']);
		$this->load->template('user_tpl',$data);
	}
	
	//用户头像
	public function avatar(){
		if (submitcheck('uploadavatar')) {
			// 上传文件
			if (isset($_FILES['avatar']['name'])) {	
				$_FILES = xss_clean($_FILES);
				$config = array();
				$res = $this->user_model->upload_avatar($this->user['uid']);
				if ($res) {
					// 更新session
					$this->security_model->update_user_session();
					showmessage('上传成功', 'setting/avatar', 2);
				}else{
					showmessage('上传失败', 'setting/avatar', 2);
				}
			}else{
				showmessage('未选择上传图片', 'setting/avatar', 2);
			}
		}
		// 设置小头像
		if(submitcheck('choose_submit')){
			$size = $this->input->post('resize', true);
			if ($size) {
				$res = $this->user_model->resize_avatar($this->user['uid'], $size);
				if ($res) {
					showmessage('设置成功', 'setting/avatar', 1);
				}else{
					showmessage('设置失败', 'setting/avatar', 2);
				}
			}
			showmessage('保存头像成功', 'home/index', 2);
		}

		$data = array();
		$this->load->template('avatar_tpl',$data);
	}
}

/* End of file index.php */
/* Location: ./application/controllers/index.php */